package view;

import domain.ATM;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;



import java.net.URL;
import java.util.ResourceBundle;

/**
 * Created by CatScan on 2016/6/21.
 */
public class MessageViewController implements ControlledStage, Initializable {
    private StageController myController;
    private ATM atm;

    @FXML
    private Label message;
    @FXML
    private Button confirm;

    public void setStageController(StageController stageController) {
        this.myController = stageController;
        switchMessage();
    }

    private void switchMessage() {
        atm = myController.getAtm();
        confirm.setOnAction(e -> {
            myController.getStage(MainApp.messageViewID).close();
        });
        String msg = "";
        switch(atm.getCurMessageType()){
            case "0": msg = "\u94f6\u884c\u5361\u65e0\u6cd5\u8bc6\u522b\uff01";break;//银行卡无法识别！
            case "1": msg = "\u5bc6\u7801\u9519\u8bef\uff01";break;//密码错误！
            case "2": msg = "\u53ea\u652f\u6301100\u5143\u7684\u9762\u989d\uff01";break;//只支持100元的面额！
            case "3": msg = "\u8d85\u8fc7\u6bcf\u6b21\u6700\u5927\u53d6\u6b3e\u91d1\u989d\u9650\u5236" + ATM.MAX_AMOUNT_PER_WITHDRAWAL + "\uff01";break;//超过每次最大取款金额限制！
            case "4": msg = "\u4f59\u989d\u4e0d\u8db3\uff01";break;//余额不足！
            case "5": msg = "\u9000\u5361\u6210\u529f\uff01";//退卡成功！
                confirm.setText("\u53d6\u5361");break;//取卡
            default: String take = atm.takeCash();
                String put = atm.putCash();
                boolean b = atm.getCurTransaction().getTransType().equalsIgnoreCase("CWD");
                confirm.setText(b? "\u53d6\u949e\n" + take: "\u653e\u949e\n" + put);//取钞,放钞
                msg = (b? "\u5df2\u51fa\u949e\uff0c\u8bf7\u53d6\u949e\uff01" : "\u8bf7\u653e\u949e\uff01");//已出钞，请取钞！,请放钞！

                if(b){
                    confirm.setOnAction(e -> {
                        myController.getStage(MainApp.messageViewID).close();
                        myController.setStage(MainApp.isToPrintReceiptViewID);
                    });
                }else {
                    confirm.setOnAction(e -> {
                        atm.enterAmount(atm.getCurTransaction().getAmount());
                        System.out.println(atm.getCurAccount().getBalance().getBalance());
                        myController.getStage(MainApp.messageViewID).close();
                        myController.setStage(MainApp.isToPrintReceiptViewID);
                    });
                }
        }
        message.setText(msg);
    }


    public void initialize(URL location, ResourceBundle resources) {
    }

    public void goToMain(){
        myController.setStage(MainApp.mainViewID);
    }


}

